var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		pageBtns();
		getSosType();
	});
});
function getSosType(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var imei = $(".students-choice-select",parent.document).attr("imei");
	var data = {
			cmd:"getCard",
			model:"card",
			userKey:userKey,
			data:{
				imei:imei,
			},
			remark:"",
	};
	AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var list = res.data;
			//显示已有的报警类型设置
			var sostype2 = $("input[name='sostype2']");
			var sostype = list[0].sostype;
			if(sostype == 4 || sostype == "4"){
				$("#sostype-close")[0].checked = true;
				sostype2.attr("disabled","disabled");
				for(var i = 0; i < sostype2.length; i++){
					sostype2[i].checked = false;
				}
			}else if(sostype != "" && sostype != "null"){
				sostype2.removeAttr("disabled");
				$("#sostype-open")[0].checked = true;
				for(var i = 0; i < sostype2.length; i++){
					if(sostype2[i].getAttribute("value") == sostype)
						sostype2[i].checked = true;
				}
			}
		}else{
			var sostype2 = $("input[name='sostype2']");
			$("#sostype-close")[0].checked = true;
			sostype2.attr("disabled","disabled");
			for(var i = 0; i < sostype2.length; i++){
				sostype2[i].checked = false;
			}
		}
	},null,null);
}
function addSosType(){
	var imei = $(".students-choice-select",parent.document).attr("imei");
	var sostype2 = "4";
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var sostype1 = $("input[name='sostype1']:checked").val();		
	if(sostype1 != 4 || sostype1 != "4"){
		sostype2 = $("input[name='sostype2']:checked").val();
	}
	var data = {
			cmd:"updateCard",
			model:"card",
			userKey:userKey,
			data:{
				imei:imei,
				sostype:sostype2,
			},
			remark:"",
	};
	processBegin("正在应用中···");
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			processEnd("设置成功");
			getSosType();
		}else{
			processError("设置失败");
		}
	},null,null);
}
function pageBtns(){
	$(".students-choice",parent.document).unbind("click");
	$(".students-choice",parent.document).click(function(){
		$(".students-choice",parent.document).removeClass("students-choice-select");
		$(this).addClass("students-choice-select");
		getSosType();
	});
	$("#sostype-set").click(function(){
		addSosType();
	});
	$("input[name='sostype1']").change(function(){
		if($(this).val() == 5){
			$("input[name='sostype2']").removeAttr("disabled");
		}else{
			$("input[name='sostype2']").attr("disabled","disabled");
		}
	});
}
function initSmPages(){
	var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
	$(".right-content").height(rcH);
	
	var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}